package com.example.hello.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                // 禁用 CSRF 保护，适用于无状态 API
                .csrf(csrf -> csrf.disable())
                // 配置访问规则，使用 requestMatchers 替换 antMatchers
                .authorizeHttpRequests(authorize -> authorize
                        .requestMatchers("/**").permitAll() // 放行 /auth/** 路径下的所有请求
                        .anyRequest().authenticated()             // 其它请求需要认证
                )
                // 启用 HTTP Basic 认证（如果需要）
                .httpBasic(Customizer.withDefaults());
        return http.build();
    }
}